﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using WHC.Security.BLL;
using WHC.OrderWater.Commons;

namespace WHC.Security.UI
{
    public class GlobalControl
    {
        public MainForm MainDialog;

        private string manageUser = "";
        public string gAppMsgboxTitle = string.Empty;   //程序的对话框标题
        public string gAppUnit = string.Empty; //单位名称
        public LicenseCheckResult LicenseResult = new LicenseCheckResult();//授权码检查的结果


        /// <summary>
        /// 存储登陆用户信息
        /// </summary>
        public string ManageUser
        {
            get { return manageUser; }
            set { manageUser = value; }
        }

        #region 基本操作函数

        /// <summary>
        /// Quits the application
        /// </summary>
        public void Quit()
        {
            Application.Exit();
        }

        /// <summary>
        /// Opens the help document.
        /// </summary>
        public void Help()
        {
            try
            {
                const string helpfile = "Help.chm";
                Process.Start(helpfile);
            }
            catch (Exception)
            {
                MessageBox.Show("文件打开失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// 关于对话框信息
        /// </summary>
        public void About()
        {
            AboutBox dlg = new AboutBox();
            dlg.StartPosition = FormStartPosition.CenterScreen;
            dlg.ShowDialog();
        }

        /// <summary>
        /// 检查用户的授权码，系统运行开始调用即可
        /// </summary>
        /// <returns></returns>
        public LicenseCheckResult CheckLicense()
        {
            LicenseCheckResult result = new LicenseCheckResult();
            string license = MyConstants.License;
            if (!string.IsNullOrEmpty(license))
            {
                try
                {
                    string decodeLicense = Base64Util.Decrypt(MD5Util.RemoveMD5Profix(license));
                    string[] strArray = decodeLicense.Split('|');
                    if (strArray.Length >= 4)
                    {
                        string componentType = strArray[0];
                        if (componentType.ToLower() == "whc.security")
                        {
                            result.IsValided = true;
                        }
                        result.Username = strArray[1];
                        result.CompanyName = strArray[2];
                        try
                        {
                            result.DisplayCopyright = Convert.ToBoolean(strArray[3]);
                        }
                        catch
                        {
                            result.DisplayCopyright = true;
                        }

                        this.LicenseResult = result;//保存授权结果到变量中，方便调用
                    }
                }
                catch
                {
                }
            }

            return result;
        }
        #endregion
    }

    public class LicenseCheckResult
    {
        /// <summary>
        /// 是否验证通过
        /// </summary>
        public bool IsValided = false;

        /// <summary>
        /// 注册的用户名称
        /// </summary>
        public string Username = "";

        /// <summary>
        /// 注册的公司名称
        /// </summary>
        public string CompanyName = "";

        /// <summary>
        /// 是否显示授权信息
        /// </summary>
        public bool DisplayCopyright = true;
    }
}
